package com.exonline.front.filter;



import com.exonline.commons.utils.SqlConditionBuilder;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.*;
import java.io.IOException;

/**
 * @author jjr
 */
@WebFilter("/*")
public class MainFilter extends HttpFilter {
    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse resp, FilterChain chain) throws IOException, ServletException {
        String uri = req.getRequestURI();
        //基础resp跨域参数
        req.setCharacterEncoding("UTF-8");
        String origin = req.getHeader("Origin");
        resp.setCharacterEncoding("UTF-8");
        resp.setHeader("Access-Control-Allow-Credentials","true");
        System.out.println(req.getHeader("Origin"));
        resp.setHeader("Access-Control-Allow-Origin", origin);
        resp.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
        resp.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH");
        resp.setContentType("application/json");
        resp.setHeader("Content-Type", "application/json");
        /**
         * @desc  实施检查 Token
         */
        //System.out.println("实施检查 Token :" + req.getHeader("Authorization"));
        //如果为空 告诉前台

        //浏览器预检请求
        if (req.getMethod().equals("OPTIONS")){
            resp.setStatus(HttpServletResponse.SC_OK);
            return;
        }

        //
        System.out.println("在线用户数:"+req.getServletContext().getAttribute("onlineUsers"));
        System.out.println(SqlConditionBuilder.buildCourseCondition(req));
        System.out.println(uri);

        chain.doFilter(req, resp);
    }
}
